package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.Homefragment.MessageThreeActvity;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.bean.MessagetwoBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

/**
 * Created by mac on 2018/1/30.
 */

public class MessagetwoAdapter extends RecyclerView.Adapter<MessagetwoAdapter.MyViewHolder> {

    private Context context;
    private List<MessagetwoBean.DataBean.ListBean>list;
    int position;


    public MessagetwoAdapter(Context context, List<MessagetwoBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MessagetwoAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.messagetwo_item,parent,false);
        return new MyViewHolder(view) ;

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.mess_tv.setText(list.get(position).getName());
        holder.mess_tv2.setText(list.get(position).getContent());
        if (list.get(position).getIsRead().equals("0")){

            holder.yuan.setVisibility(View.GONE);
        }else {
            holder.yuan.setVisibility(View.VISIBLE);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MessageThreeActvity.class);
                intent.putExtra("parentId",list.get(position).getId());
                Log.e("IDIDIDIIDIDID",list.get(position).getId());
                context.startActivity(intent);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                AlertDialog alertDialog = new AlertDialog.Builder(context).setTitle("提示").setMessage("是否需要删除？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                OkHttpUtils.post().url("http://develop.jhjvip.cn:8090/mall/mobile/msg/del")
                                        .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                                        .addParams("appId", AUniqueIdentifier.getDeviceId(context))
                                        .addParams("msgId",list.get(position).getId())
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {

                                            }
                                            @Override
                                            public void onResponse(String response) {
                                                ToastUtils.showShort("删除成功");
                                                notifyDataSetChanged();
                                            }
                                        });


                            }
                        }).create();
                alertDialog.show();
                return true;
            }
        });


    }


    @Override
    public int getItemCount() {
        return list.size();
    }



    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView mess_tv,mess_tv2;
        TextView yuan;
        public MyViewHolder(View itemView) {
            super(itemView);

            mess_tv = (TextView) itemView.findViewById(R.id.messagetwo_tv1);
            mess_tv2 = (TextView) itemView.findViewById(R.id.messagetwo_tv2);
            yuan = (TextView) itemView.findViewById(R.id.two_yuan);

        }
    }


    public interface OnItemClickListener {
        void onitem(int position);


    }
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


}
